home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS11.ADF
/
Modula-2
/
CaseConvert
/
scanner.mod
< prev
next >
Wrap
Text File
|
1986-08-05
|
3KB
|
99 lines
(* Implementation of a simple scanner.
Created: 5/15/86 by RB
Modified:
Copyright (c) 1986 - by Richie Bielak
This program maybe freely copied, but please leave my name in.
Thanks....Richie
*)
IMPLEMENTATION MODULE Scanner;
FROM InOut IMPORT Read, Write, Done;
VAR
LastCh : CHAR;
(* Get next character from the input, return 0C at EOF *)
PROCEDURE GetChar (VAR ch : CHAR) : CHAR;
BEGIN
IF LastCh <> 0C THEN
ch := LastCh; LastCh := 0C;
ELSE
Read (ch); IF NOT Done THEN ch := 0C END;
END;
RETURN ch
END GetChar;
(* Put a character back in the input stream *)
PROCEDURE PushBack (VAR ch : CHAR);
BEGIN
LastCh := ch; ch := 0C;
END PushBack;
(* Get the next token from the input stream *)
PROCEDURE GetToken (VAR Token : TokenType;
VAR Value : ARRAY OF CHAR) : BOOLEAN;
TYPE
StateType = (Scan, OpenPren, Asterisk, id);
CharSet = SET OF CHAR;
VAR
State : StateType;
ch : CHAR;
i : CARDINAL;
MoreInput : BOOLEAN;
BEGIN
State := Scan; i := 0; Token := Null; MoreInput := TRUE;
(* Do this until we find a token, or hit EOF *)
WHILE (Token = Null) AND (MoreInput) DO
(* Get another character from the input *)
IF GetChar (ch) = 0C THEN MoreInput := FALSE
ELSE
CASE State OF
Scan: IF ch = '(' THEN State := OpenPren
ELSIF ch = '*' THEN State := Asterisk
ELSIF ch = '"' THEN Token := DoubleQ
ELSIF ch = "'" THEN Token := SingleQ
ELSIF CAP(ch) IN CharSet{'A'..'Z'} THEN State := id
END;
|
OpenPren: IF ch = "*" THEN
Token := OpenComment
ELSE
State := Scan; PushBack (ch)
END
|
Asterisk: IF ch = ")" THEN
Token := CloseComment
ELSE
State := Scan; PushBack (ch)
END
|
id: IF CAP (ch) IN CharSet{'A'..'Z', '0'..'9'} THEN
(* Do nothing *)
ELSE
Token := Identifier; PushBack (ch)
END
END; (* CASE *)
(* Output current character, or store in returned value *)
(* Note: only identifiers are stored. *)
IF (State = id) AND (ch <> 0C) THEN
Value[i] := ch; INC (i)
ELSIF ch <> 0C THEN
Write (ch)
END (* IF *)
END (* IF *)
END; (* WHILE *)
(* Mark the end of value with a null *)
Value [i] := 0C;
RETURN (Token <> Null)
END GetToken;
BEGIN
LastCh := 0C;
END Scanner.